module(..., package.seeall)

new = function ()
	print("Mem: "..(system.getInfo("textureMemoryUsed")/1000000).." mb");
	print("level1")
	
	local physics = require("physics")
	physics.start()
	--physics.setDrawMode("hybrid");
	
	local hud = display.newGroup();
	local game = display.newGroup();
	

	display.setStatusBar( display.HiddenStatusBar )	
	game.x = 0

	local sky = display.newImage( "sky.png", true )
	game:insert( sky )
	sky.x = 160; sky.y = 160

	local sky2 = display.newImage( "sky.png", true )
	game:insert( sky2 )
	sky2.x = 1120; sky2.y = 160

	local grass = display.newImage( "grass.png", true )
	game:insert( grass )
	grass.x = 160
	grass.y = 340
	physics.addBody( grass, "static", { friction=1, bounce=0 } )

	local grass2 = display.newImage( "grass.png", true )
	game:insert( grass2 )
	grass2.x = 1120
	grass2.y = 340
	physics.addBody( grass2, "static", { friction=1, bounce=0 } )

	rampa = display.newImage("roof.png")
	game:insert(rampa)
	physics.addBody(rampa,"static");
	rampa.x = 450;
	rampa.y = 280;
	rampa.rotation = -30;
	
	local pauseButton = display.newImageRect( "pause.png", 128, 128 )
	pauseButton.id = "pause";
	pauseButton.x = 450;
	pauseButton.y = 30;
	pauseButton.xScale = 0.5;
	pauseButton.yScale = 0.5;
	
	local restartButton = display.newImageRect( "restart.png", 128, 128 )
	restartButton.id = "restart";
	restartButton.x = 400;
	restartButton.y = 30;
	restartButton.xScale = 0.5;
	restartButton.yScale = 0.5;
	
	local function listener(event) 
		if (event.phase == "ended") then
			if (event.target.id == "pause") then				
				pauseScreen = require("GamePauseScr");
				pauseScreen.setOriginLevel("GameLevel1");
				director:changeScene("GamePauseScr", "crossfade");
			end
			if (event.target.id == "restart") then
				director:changeScene("GameLevel1", "crossfade");
			end			
		end
		return true 
	end
	
	endLevel = function()
		print(" End level ");
	
		--scoreScreen = require("GameScoreScreen");
		--scoreScreen.setOriginLevel("GameLevel1");
		--director:changeScene("GameScoreScreen", "crossfade");
	
	end
	
	pauseButton:addEventListener("touch", listener);	
	restartButton:addEventListener("touch", listener);	
	
	
	local myText = display.newText("Game Level 1", 0, 0, native.systemFont, 16)
	myText:setTextColor(255, 255, 255)
	hud:insert(myText);
	hud:insert(pauseButton);
	hud:insert(restartButton);
	car = require("GameCar");
	car.newCar(game,hud,endLevel);
	
	return game;	
end